package com.sensePlusVR.bean;

import java.util.ArrayList;
import java.util.List;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.MyLog;
/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午2:56:49
 * @desc:Game item 
 *
 */
@Table(name = "GameItem")
public class GameItem/* extends EntityBase*/ implements Parcelable {
	private static final String TAG = GameItem.class.getSimpleName();
	/**
	 * 
	 */

	
	@Column(name = "id",isId = true,autoGen = false)
	private int id;
	
	@Column(name = "tags")
	private String tags;

	@Column(name = "gameurl")
	private String gameurl;
	
	@Column(name = "gameurl2")
	private String gameurl2;

	@Column(name = "title")
	private String title;

	@Column(name = "level")
	private String level;

	@Column(name = "price")
	private float price;

	@Column(name = "area")
	private String area;

	@Column(name = "summary")
	private String summary;

	@Column(name = "actors")
	private String actors;

	@Column(name = "rating")
	private float rating;

	@Column(name = "type")
	private String type;

	@Column(name = "imgurl")
	private String imgurl;

	@Column(name = "company")
	private String company;

	@Column(name = "directors")
	private String directors;
	
	@Column(name = "videoid")
	private int videoid;
	
	@Column(name = "createtime")
	private String createtime;
	
	@Column(name = "videourl")
	private String videourl;
	
	@Column(name = "grade")
	private String grade;
	
	@Column(name = "colloctoned")
	private int colloctoned;
	
	@Column(name = "usercollectioncount")
	private int usercollectioncount;
	
	@Column(name = "version")
	private String version;
	
	@Column(name = "format")
	private String format;
	
	@Column(name = "prefix")
	private String prefix;
	
	@Column(name = "coin")
	private Integer coin;
	
	@Column(name = "discountcoin")
	private Integer discountcoin;

	@Column(name = "isfree")
	private Integer isfree;
	
	@Column(name = "size")
	private long size;
	
	@Column(name = "isPraise")
	private Integer isPraise;
	
	@Column(name = "praisecount")
	private Integer praisecount;
	
	private ActorItem actoritem = new ActorItem();



	public GameItem() {
		
	}
	
	public GameItem(Parcel in){
		id = in.readInt();
		tags = in.readString();
		gameurl = in.readString();
		gameurl2 = in.readString();
		title = in.readString();
		level = in.readString();
		price = in.readFloat();
		area = in.readString();
		summary = in.readString();
		actors = in.readString();
		rating = in.readFloat();
		type = in.readString();
		imgurl = in.readString();
		company = in.readString();
		directors = in.readString();
		videoid = in.readInt();
		createtime = in.readString();
		videourl = in.readString();
		grade = in.readString();
		colloctoned = in.readInt();
		usercollectioncount = in.readInt();
		version = in.readString();
		format = in.readString();
		prefix = in.readString();
		coin = in.readInt();
		discountcoin = in.readInt();
		isfree = in.readInt();
		size = in.readLong();
		isPraise = in.readInt();
		praisecount = in.readInt();
		actoritem = in.readParcelable(ActorItem.class.getClassLoader());
	}


	@Override
	public String toString() {
		return "GameItem [id=" + id + ", tags=" + tags + ", gameurl=" + gameurl
				+ ", gameurl2=" + gameurl2 + ", title=" + title + ", level="
				+ level + ", price=" + price + ", area=" + area + ", summary="
				+ summary + ", actors=" + actors + ", rating=" + rating
				+ ", type=" + type + ", imgurl=" + imgurl + ", company="
				+ company + ", directors=" + directors + ", videoid=" + videoid
				+ ", createtime=" + createtime + ", videourl=" + videourl
				+ ", grade=" + grade + ", colloctoned=" + colloctoned
				+ ", usercollectioncount=" + usercollectioncount + ", version="
				+ version + ", format=" + format + ", prefix=" + prefix
				+ ", coin=" + coin + ", discountcoin=" + discountcoin
				+ ", isfree=" + isfree + ", size=" + size + ", isPraise="
				+ isPraise + ", praisecount=" + praisecount + ", actoritem="
				+ actoritem + "]";
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public String getGameurl() {
		return gameurl;
	}

	public void setGameurl(String gameurl) {
		this.gameurl = gameurl;
	}

	public String getGameurl2() {
		return gameurl2;
	}

	public void setGameurl2(String gameurl2) {
		this.gameurl2 = gameurl2;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getActors() {
		return actors;
	}

	public void setActors(String actors) {
		this.actors = actors;
	}

	public float getRating() {
		return rating;
	}

	public void setRating(float rating) {
		this.rating = rating;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getImgurl() {
		return imgurl;
	}

	public void setImgurl(String imgurl) {
		this.imgurl = imgurl;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getDirectors() {
		return directors;
	}

	public void setDirectors(String directors) {
		this.directors = directors;
	}

	public int getVideoid() {
		return videoid;
	}

	public void setVideoid(int videoid) {
		this.videoid = videoid;
	}

	public String getCreatetime() {
		return createtime;
	}

	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}

	public String getVideourl() {
		return videourl;
	}

	public void setVideourl(String videourl) {
		this.videourl = videourl;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public int getColloctoned() {
		return colloctoned;
	}

	public void setColloctoned(int colloctoned) {
		this.colloctoned = colloctoned;
	}

	public int getUsercollectioncount() {
		return usercollectioncount;
	}

	public void setUsercollectioncount(int usercollectioncount) {
		this.usercollectioncount = usercollectioncount;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public ActorItem getActoritem() {
		return actoritem;
	}

	public void setActoritem(ActorItem actoritem) {
		this.actoritem = actoritem;
	}
	

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}
	
	public Integer getCoin() {
		return coin;
	}

	public void setCoin(Integer coin) {
		this.coin = coin;
	}

	public Integer getDiscountcoin() {
		return discountcoin;
	}

	public void setDiscountcoin(Integer discountcoin) {
		this.discountcoin = discountcoin;
	}

	public Integer getIsfree() {
		return isfree;
	}

	public void setIsfree(Integer isfree) {
		this.isfree = isfree;
	}


	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public Integer getIsPraise() {
		return isPraise;
	}

	public void setIsPraise(Integer isPraise) {
		this.isPraise = isPraise;
	}

	public Integer getPraisecount() {
		return praisecount;
	}

	public void setPraisecount(Integer praisecount) {
		this.praisecount = praisecount;
	}

	public static void saveOrUpdate(Object object) {
		try {
    		Context context = VRApplication.getAppContext();
    		if(context != null) {
    			DbManager db = VRApplication.getDbManager();
    			db.saveOrUpdate(object);
    		}
    	}catch(Exception e){
    		MyLog.e(TAG, "GameItem update failed: "+e);
    	}
	}
	
	public static GameItem getFirstGame(){
		Context context = VRApplication.getAppContext();
		if(context != null){
			DbManager db = VRApplication.getDbManager();
			try {
				return db.findFirst(GameItem.class);
			} catch (DbException e) {
				MyLog.e(TAG, "getFirstGame findFirst failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static GameItem getCreateTimeGame(String createtime){
		Context context = VRApplication.getAppContext();
		if(context != null){
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(GameItem.class).where("createtime", "=", createtime).findFirst();
			} catch (DbException e) {
				MyLog.e(TAG, "getCreateTimeGame findFirst failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static List<GameItem> getGameListSort(boolean desc){
		Context context = VRApplication.getAppContext();
		List<GameItem> newList = new ArrayList<GameItem>();
		if(context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				List<GameItem> gameList = db.selector(GameItem.class).orderBy("createtime", desc).findAll();
				if(gameList == null) return null;
				for(GameItem gItem : gameList){
					if(GameItem.isRealGame(gItem)){
						newList.add(gItem);
					}else{
						db.delete(gItem);
					}
				}
				return newList;
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListSort failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static boolean isRealGame(GameItem gameItem){
		
		boolean isRealGame = gameItem.getType().equals(Constant.TYPE_REAL)
		|| gameItem.getType().equals(Constant.TYPE_DEMO_REAL);
		
		return isRealGame;
		
	}
	
	public static List<GameItem> getGameListByIds(List<Integer> ids){
		MyLog.d(TAG, "getGameListByIds ids : " + ids.toString());
		Context context = VRApplication.getAppContext();
		int idsCount = ids!=null?ids.size():0;
		if(context != null&&idsCount!=0) {
			DbManager db = VRApplication.getDbManager();
			List<GameItem> listItem = new ArrayList<GameItem>();
			try {
				for (Integer id:ids){
					GameItem gameItem = db.selector(GameItem.class).where(WhereBuilder.b("id", "=", id)).findFirst();
					if(gameItem != null){
						if(GameItem.isRealGame(gameItem))
						listItem.add(gameItem);
					}
				}
				MyLog.d(TAG, "listItem listItem : " + listItem);
				return listItem;
				
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListByIds failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static long getGameCount(){
		Context context = VRApplication.getAppContext();
		if(context != null) {
			DbManager db = VRApplication.getDbManager();
			try {

				return db.selector(GameItem.class).count();
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListSort failed: "+e);
				return 0;
			}
		}
		return 0;
	}
	
	
	public static GameItem getGameById(int gameId){
		Context context = VRApplication.getAppContext();
		if(context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(GameItem.class).where("id", "=", gameId).findFirst();
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListSort failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static List<GameItem> getGameListByActorName(String actorName){
		Context context = VRApplication.getAppContext();
		if(context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(GameItem.class).where("actors", "=", actorName).findAll();
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListSort failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static List<GameItem> getGameListByActorName(String actorName,boolean desc){
		Context context = VRApplication.getAppContext();
		if(context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(GameItem.class).where("actors", "=", actorName).orderBy("createtime", desc).findAll();
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListSort failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static List<GameItem> getGameListSort(boolean desc,int limit,int offset){
		Context context = VRApplication.getAppContext();
		List<GameItem> newList = new ArrayList<GameItem>();
		if(context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				List<GameItem> gamelist = db.selector(GameItem.class).orderBy("createtime", desc).limit(limit).offset(offset).findAll();
				if(gamelist == null) return null;
				MyLog.d(TAG, "getGameListSort gamelist size : " + gamelist.size());
				for(GameItem gItem : gamelist){
					if(GameItem.isRealGame(gItem)){
						newList.add(gItem);
					}else{
						db.delete(gItem);
					}
				}
				MyLog.d(TAG, "getGameListSort newList size : " + newList.size() + " gamelist size : " + gamelist.size());
				return newList;
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListSort failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static void updateGameList(List<GameItem> list){
		Context context = VRApplication.getAppContext();
		if(context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				db.delete(GameItem.class);
			} catch (DbException e) {
				MyLog.e(TAG, "updateGameList delete failed: "+e);
			}
			try {
				db.saveOrUpdate(list);
			} catch (DbException e) {
				MyLog.e(TAG, "updateGameList saveOrUpdate failed: "+e);
			}
		}
	}
	
	
	public static boolean isVROnly(GameItem gameItem){
		
		String tag = gameItem.getTags();
		
		if(tag == null)return false;
		
		MyLog.d(TAG, "tag : " + tag);
		
		return tag.contains(":vronly:");
		
		
	}
	
//	public static boolean is2DOnly(GameItem gameItem){
//		
//		String tag = gameItem.getTags();
//		
//		if(tag == null)return false;
//		
//		MyLog.d(TAG, "tag : " + tag);
//		
//		return tag.contains(":2donly:");
//	}
	
	public static boolean isVRFormat(GameItem gameItem){
		String format = gameItem.getFormat();
		if(format == null)return false;
		return format.equalsIgnoreCase("VR");
	}
	
	public static boolean is2DFormat(GameItem gameItem){
		String format = gameItem.getFormat();
		if(format == null)return false;
		return format.equalsIgnoreCase("2D");
	}
	
	
	public static void deleteGameItem(GameItem gameItem){
		Context context = VRApplication.getAppContext();
		if(context != null){
			DbManager db = VRApplication.getDbManager();
			try {
				//db.deleteById(GameItem.class, gameItem);
				db.delete(gameItem);
			} catch (DbException e) {
				MyLog.e(TAG, "deleteGameItem delete failed: "+e);
			}
		}
		
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		
		out.writeInt(id);
		out.writeString(tags);
		out.writeString(gameurl);
		out.writeString(gameurl2);
		out.writeString(title);
		out.writeString(level);
		out.writeFloat(price);
		out.writeString(area);
		out.writeString(summary);
		out.writeString(actors);
		out.writeFloat(rating);
		out.writeString(type);
		out.writeString(imgurl);
		out.writeString(company);
		out.writeString(directors);
		out.writeInt(videoid);
		out.writeString(createtime);
		out.writeString(videourl);
		out.writeString(grade);
		out.writeInt(colloctoned);
		out.writeInt(usercollectioncount);
		out.writeString(version);
		out.writeString(format);
		out.writeString(prefix);
		out.writeInt(coin);
		out.writeInt(discountcoin);
		out.writeInt(isfree);
		out.writeLong(size);
		out.writeInt(isPraise);
		out.writeInt(praisecount);
		out.writeParcelable(actoritem, flags);
		
	}
	
	public static final Parcelable.Creator<GameItem> CREATOR = new Creator<GameItem>() {
		
		@Override
		public GameItem[] newArray(int size) {
			// TODO Auto-generated method stub
			return new GameItem[size];
		}
		
		@Override
		public GameItem createFromParcel(Parcel in) {
			// TODO Auto-generated method stub
			return new GameItem(in);
		}
	};

	
}
